package com.fla.aiba.api;

import com.fla.aiba.entity.user.CardAuthentication;
import com.fla.aiba.repository.CardAuthenticationRepository;
import com.fla.aiba.util.UploadUtil;
import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.smartcardio.Card;

/**
 * Created by qudingyi on 2017/4/25.
 */
@RequestMapping("/api/v1/authentication")
public class AuthenticationAPIContller {

    @Autowired
    private CardAuthenticationRepository cardAuthenticationRepository;

    /**
     * 身份认证
      @return
     * @throws ServiceException
     */
    @RequestMapping(value="/card",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object cardAuthentication(CardAuthentication cardAuthentication,@RequestParam("file") CommonsMultipartFile[] files)throws ServiceException {
        try {
            for (int i=0,length=files.length;i<length;i++) {
                String path= UploadUtil.copyFile(files[i],"card_authentication");
                switch (i){
                    case 0:
                        cardAuthentication.setBeforeImg(path);
                        break;
                    case 1:
                        cardAuthentication.setAfterImg(path);
                        break;
                    default:
                        cardAuthentication.setShouChiImg(path);
                        break;
                }

            }
            cardAuthenticationRepository.save(cardAuthentication);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }

    }


}
